import { useModel } from '@umijs/max';
import { useMemo } from 'react';
import { hashCode } from 'web-plugin-utils';

// @ts-ignore
export default () => {
  // @ts-ignore
  const { initialState } = useModel('@@initialState');

  // @ts-ignore
  const videoMatrixServer = useMemo<string | undefined>(() => {
    if (initialState?.videoMatrixIp) {
      return `http://${initialState?.videoMatrixIp}`;
    } else {
      return undefined;
    }
  }, [initialState]);

  const peerServer = useMemo<string | undefined>(() => {
    if (initialState?.serverIp) {
      return `http://${initialState?.serverIp}:9999`;
    } else {
      return undefined;
    }
  }, [initialState]);

  const liveKitServer = useMemo<string | undefined>(() => {
    if (initialState?.serverIp) {
      return `http://${initialState?.serverIp}:7880`;
    } else {
      return undefined;
    }
  }, [initialState]);

  const liveKitWsServer = useMemo<string | undefined>(() => {
    if (initialState?.serverIp) {
      return `ws://${initialState?.serverIp}:7880`;
    } else {
      return undefined;
    }
  }, [initialState]);

  const streamServer = useMemo<string | undefined>(() => {
    if (initialState?.serverIp) {
      return `http://${initialState?.serverIp}:8080`;
    } else {
      return undefined;
    }
  }, [initialState]);

  const panServer = useMemo<string | undefined>(() => {


    if (initialState?.serverIp) {
      return `http://${initialState?.serverIp}:8089`;
    } else {
      return undefined;
    }
  }, [initialState]);
  // @ts-ignore
  const peerId = useMemo<string | undefined>(() => {
    if (initialState?.nickname) {
      return String(hashCode(initialState.nickname!));
    } else {
      return undefined;
    }
  }, [initialState]);

  const wsServer = useMemo<string | undefined>(() => {
    if (initialState?.serverIp) {
      return `ws://${initialState?.serverIp}:9999`;
    } else {
      return undefined;
    }
  }, [initialState]);

  const srsServer = useMemo<string | undefined>(() => {
    if (initialState?.serverIp) {
      return `http://${initialState?.serverIp}:1985/rtc/v1/whip`;
    } else {
      return undefined;
    }
  }, [initialState]);

  const m7sServer = useMemo<string | undefined>(() => {
    if (initialState?.m7sServerIp) {
      return `http://${initialState?.m7sServerIp}:8089`;
    } else {
      return undefined;
    }
  }, [initialState]);

  const obsPushServer = useMemo<string | undefined>(() => {
    if (initialState?.obsPushServer) {
      return `rtmp://${initialState?.obsPushServer}`;
    } else {
      return undefined;
    }
  }, [initialState]);

  return {
    peerId,
    panServer,
    videoMatrixServer,
    peerServer,
    streamServer,
    liveKitServer,
    liveKitWsServer,
    wsServer,
    srsServer,
    m7sServer,
    obsPushServer
  };
};
